package history.link.singlylinklist;

import java.util.Iterator;

/**
 * @author czt
 * @version 1.0
 * @since 2024/10/24
 *
 * 单向链表
 */
public class SinglyLinkedList implements Iterable<Integer>{

    private Node head = null; //头指针


    /**
     * 节点类
     */
    private static class Node{
        int value; //值

        Node next; //下一个节点指针

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    //向链表头部添加数据
    public void addFirst(int value) {
//        head = new Node(value, null);
        // 1.链表为空 2.链表非空
        head = new Node(value, head);
    }

    //遍历链表
    public void loop() {
        //当前要处理的节点
        Node pointer = head;

        //当节点为空时，说明已经到了最后的节点，退出循环
        while (pointer != null) {
            System.out.println("pointer = " + pointer.value);
            //让节点指向下一个节点
            pointer = pointer.next;
        }
    }

    //遍历链表
    public void loop2() {
        for (Node p = head; p != null; p = p.next) {
            System.out.println("p = " + p.value);
        }
    }

    public void loop3() {
        recursion(head);
    }

    //递归
    private void recursion(Node curr) {//某个节点要进行的操作
        if (curr == null) {
            return;
        }
        System.out.println(curr.value);
        recursion(curr.next);
    }

    //遍历
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head;
            @Override
            public boolean hasNext() {
                return p != null;
            }

            @Override
            public Integer next() {
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    //查找链表最后一个节点
    private Node findLast() {
        if (head == null) {
            return null;
        }

        Node p;
        for (p = head; p.next != null; p = p.next) {

        }
        return p;
    }

    //向链表尾部添加节点
    public void addLast(int value) {
        //先找到最后一个节点
        Node last = findLast();

        //如果为空，则相当于在头部插入节点
        if (last == null) {
            head = new Node(value, null);
            return;
        }

        //让最后一个节点的下一个指针指向新节点
        last.next = new Node(value, null);
    }

    //根据索引获取节点
    private Node findNode(int index) {
        int i = 0; //使用i代表节点中的索引值，index为要查找的索引
        for (Node p = head; p != null; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        //没有找到
        return null;
    }

    //返回要查找的节点值
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw new IllegalArgumentException();
        }
        return node.value;
    }

    //任意位置插入节点
    public void insert(int index, int value) {
        if (index == 0) {
            //相当于在头部插入节点
            head = new Node(value, head);
            return;
        }

        Node prev = findNode(index - 1); //要插入节点的上一个节点

        //Node next = findNode(index); //要插入节点的下一个节点(因为此时还没有插入，所以不需要+1)

        if (prev == null) {
            throw new IllegalArgumentException();
        }

        prev.next = new Node(value, prev.next);
    }

    //删除头节点
    public void removeFirst() {
        //节点为空
        if (head == null) {
            throw new IllegalArgumentException();
        }
        head = head.next;
    }

    //按索引删除
    public void remove(int index) {
        if (index == 0) {
            head = head.next;
            return;
        }

        Node prev = findNode(index - 1); //上一个节点

        if (prev == null) {
            throw new IllegalArgumentException();
        }

        Node removed = prev.next; //被删除的节点

        if (removed == null) {
            throw new IllegalArgumentException();
        }
        prev.next = removed.next;
    }
}


